﻿using UnityEngine;
using System.Collections;

public class Utility : MonoBehaviour {
	public GameObject ref_damageNumber;
	public GameObject pool_damageNumber;
	public Sprite[] damageNumberSprites;

	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () {
	}
	//assuming damage will not be more than 999,999,999 (nine digits)
	public void displayDamageNumber(string dmgString, Vector2 coord){

		float totalStringWidth = (dmgString.Length-1f)/2f;
		totalStringWidth *= 0.6f;
		Vector2 numberPosition = new Vector2( (coord.x-totalStringWidth), coord.y);
		int sprArrayIndex = 0;
		foreach(char singleDigitStr in dmgString.ToCharArray()){
			switch(singleDigitStr)
			{
			case '0':
				sprArrayIndex = 0;
				break;
			case '1':
				sprArrayIndex = 1;
				break;
			case '2':
				sprArrayIndex = 2;
				break;
			case '3':
				sprArrayIndex = 3;
				break;
			case '4':
				sprArrayIndex = 4;
				break;
			case '5':
				sprArrayIndex = 5;
				break;
			case '6':
				sprArrayIndex = 6;
				break;
			case '7':
				sprArrayIndex = 7;
				break;
			case '8':
				sprArrayIndex = 8;
				break;
			case '9':
				sprArrayIndex = 9;
				break;
			}

			GameObject newObj = (GameObject)Instantiate(ref_damageNumber);
			newObj.transform.parent = pool_damageNumber.transform;
			newObj.AddComponent<DamageNumberFloating>().displayNumber(numberPosition, damageNumberSprites[sprArrayIndex]);
			numberPosition.x += 0.6f;
		}
	}

	public static GameObject duplicateNewObj(string name, Vector2 coord, GameObject objForDuplication)
	{
		GameObject newObj = null;
		try
		{
			newObj = (GameObject)Instantiate(objForDuplication, coord, Quaternion.identity);
			newObj.name = name;
			newObj.GetComponent<SpriteRenderer>().sortingOrder = 1;
			SpriteRenderer sprRenderer = newObj.GetComponent<SpriteRenderer>();
			if(sprRenderer == null)
			{
				Debug.Log ("sprRenderer is null");
			}
		}
		catch(UnityException ue)
		{
			Debug.Log("UnityException caught: " + ue.Message);
		}
		
		return newObj;
	}

	public static void clearAllObjs(ArrayList allObjs){
		foreach(GameObject obj in allObjs){
			Destroy(obj);
		}
		allObjs.Clear();
	}
}
